﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Web.Script.Serialization;
using System.Xml.Linq;
//
using PerceptiveMCAPI.Types.Internal;
//
namespace PerceptiveMCAPI.Types
{
	public class listBatchSubscribeOutput : Api_BaseOutput
	{
		public listBatchSubscribeResults result { get; set; }
		public listBatchSubscribeParms inputParms { get; private set; }
		// --------------------------------------------------------
		public listBatchSubscribeOutput( listBatchSubscribeInput input )
			: base( input )
		{
			inputParms = input.parms;
			this.result = new listBatchSubscribeResults();
		}
		// --------------------------------------------------- tostring
		public override string ToString()
		{
			return string.Format( "Added: {0}, Updated: {1}, Errors: {2}",
				result.add_count, result.update_count, result.error_count );
		}

		#region ================================================================== format setup

		// ---- serial
		internal void Format( string api_Request, string api_Response )
		{
			this.api_Request = api_Request;
			this.api_Response = api_Response;
			// ----------------------------------
			if ( this.api_OutputType == EnumValues.OutputType.JSON )
				outputJSON();
			else if ( this.api_OutputType == EnumValues.OutputType.XML )
				outputXML();
		}
		// ---- xmlrpc
		internal void Format( x_listBatchSubscribe rpcResult )
		{
			this.api_Request = EnumValues.NotSupplied;
			this.api_Response = EnumValues.NotSupplied;
			// ------------------------
			this.result = FormatResults( rpcResult );
		}
		#endregion

		#region ================================================================= Output
		// ---------------------------------------------------------- outputXML
		protected void outputXML()
		{
			XElement xml = XElement.Parse( this.api_Response );
			// error check
			Api_Error error = apiSerial.GetXmlOutputError( xml );
			if ( error != null )
			{
				this.api_ErrorMessages.Add( error );
				return;
			}
			// -------------------------------------------- get values
			try
			{
				int i;
				listBatchSubscribeResults results = new listBatchSubscribeResults();
				int.TryParse( xml.Element( "add_count" ).Value, out i ); results.add_count = i;
				int.TryParse( xml.Element( "update_count" ).Value, out i ); results.update_count = i;
				int.TryParse( xml.Element( "error_count" ).Value, out i ); results.error_count = i;
				IEnumerable<XElement> errors = xml.Elements( "errors" ).Elements( "struct" );
				foreach ( XElement entry in errors )
				{
					listBatchSubscribeResults.Error err = new listBatchSubscribeResults.Error();
					int.TryParse( entry.Element( "code" ).Value, out i ); err.code = i;
					err.message = entry.Element( "message" ).Value;
					err.email = entry.Element( "email" ).Value;
					results.errors.Add( err );
				}
				this.result = results;
			}
			catch ( Exception ex )
			{
				this.api_ErrorMessages.Add( apiException.FormatError( ex ) );
			}
		}
		// ---------------------------------------------------------- outputJSON
		protected void outputJSON()
		{
			JavaScriptSerializer jss = new JavaScriptSerializer();

			try
			{
				if ( this.api_Response.Contains( EnumValues.JSONErrorString ) )
				{
					Api_Error err = jss.Deserialize<Api_Error>( this.api_Response );
					this.api_ErrorMessages.Add( err );
				}
				else
				{
					x_listBatchSubscribe jsonResult = jss.Deserialize<x_listBatchSubscribe>( this.api_Response );
					this.result = FormatResults( jsonResult );
				}
			}
			catch ( Exception ex )
			{
				this.api_ErrorMessages.Add( apiException.FormatError( ex ) );
			}
		}
		// ---------------------------------------------------------- FormatReults
		private listBatchSubscribeResults FormatResults( x_listBatchSubscribe x_results )
		{
			listBatchSubscribeResults results = new listBatchSubscribeResults();
			//
			results.add_count = x_results.add_count;
			results.update_count = x_results.update_count;
			results.error_count = x_results.error_count;
			foreach ( var err in x_results.errors )
			{
				listBatchSubscribeResults.Error apierr = new listBatchSubscribeResults.Error();
				apierr.code = err.code;
				apierr.message = err.message;
				apierr.email = err.email;
				//
				results.errors.Add( apierr );
			}
			//
			return results;
		}

		#endregion
	}
}
